<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="disabled">
      <div slot="detail" class="table-page-search-wrapper">
        <a-form-model layout="inline" ref="ruleForm" :model="queryParam">
          <z-form-item :datas="fieldArr" :form="queryParam" :submitAction="{ show: true, toggle: false, span: 8 }"
            @onSubmit="onQuery" @onReset="onResetForm" />
        </a-form-model>
        <z-table ref="table" rowKey="id" selectionType="radio" :showAlert="false" :showSelection="true"
          :loading="loading" :columns="columns" :dataSource="pageData" @selectedRow="onSelectedRow"
          @change="onTableChange" />
      </div>
    </j-form-container>
  </a-spin>
</template>

<script>
import { cityReferralApply, personDistribution, socialDistribution } from '@/api/sjsk/personService'
// import { getAction } from '@/api/manage'
import { getDivisionAdmin } from "@api/sjsk/specialist";
export default {
  props: {
    componentKey: {
      type: String,
      required: true
    },
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      confirmLoading: false,
      loading: false,
      queryParam: {},
      fieldArr: [
        { label: '姓名', prop: 'personnelName', tagType: 'input', span: 8 },
        { label: '联系电话', prop: 'phone', tagType: 'input', span: 8 },
      ],
      columns: [
        {
          title: '序号', dataIndex: 'order', width: 40, align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize +
            (index + 1)}` //序号分页后连续
        },
        {
          title: '姓名',
          dataIndex: 'personnelName',
          width: 100,
          align: 'center',
          customRender: (text) => {
            return <j-ellipsis value={text} length={14} />
          },
        },
        {
          title: '所属单位',
          dataIndex: 'region',
          width: 140,
          align: 'center',
          customRender: (text) => {
            return text ? <j-ellipsis value={text} length={14} /> : '--'
          },
        },
        {
          title: '联系电话',
          dataIndex: 'phone',
          width: 80,
          align: 'center',
          customRender: (text) => {
            return text ? text : '--'
          }
        },
      ],
      controlPersonType: 1,
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      pageData: {},
      departmentId: ""
    }
  },
  methods: {
    show(record, departmentId) {
      // console.log("departmentId")
      // console.log(departmentId)
      this.departmentId = departmentId
      // 社会面-人员分配
      if (this.componentKey === 'personnel-assignment' && record.controlPersonType === 2) {
        this.$set(this.model, 'id', record.id)
        this.controlPersonType = 2
        // 社戒社康-人员分配
      } else if (this.componentKey === 'personnel-assignment') {
        this.$set(this.model, 'controlPersonId', record.controlPersonId)
        this.$set(this.model, 'controlPersonType', 1) //管控人员类型 1：社戒社康 2：社会面人员
        this.$set(this.model, 'controlType', 2) //异地管控类型 1：省内异地管控 2：省外异地管控
        this.controlPersonType = 1
        // 同城转介
      } else if (this.componentKey === 'intra-city-referral') {
        // console.log("11111111111")
        // console.log(record)
        this.$set(this.model, 'sjskPersonId', record.id)
        this.$set(this.model, 'oldBelongManagerId', record.belongManagerId)
        this.$set(this.model, 'oldBelongManagerName', record.belongManagerName)
        this.$set(this.model, 'oldBelongManagerPhone', record.belongManagerPhone)
      }
      console.log(this.model)
      this.onQuery()
    },
    submitForm() {
      if (Object.keys(this.model).length > 0 && !this.model.belongManagerId) {
        this.$message.warn('请选择其中一条！')
        return
      }
      const parameter = Object.assign({}, this.model)
      this.confirmLoading = true
      let api
      // 社会面-人员分配
      if (this.componentKey === 'personnel-assignment' && this.controlPersonType === 2) {
        api = socialDistribution(parameter)
        // 社戒社康-人员分配
      } else if (this.componentKey === 'personnel-assignment' && this.controlPersonType === 1) {
        api = personDistribution(parameter)
        // 同城转介
      } else if (this.componentKey === 'intra-city-referral') {
        api = cityReferralApply(parameter)
      }
      if (!api) return
      api.then(res => {
        this.confirmLoading = false
        if (res.success) {
          this.$message.success(res.message)
          this.$emit('ok');
        } else {
          this.$message.warn(res.message)
        }
      })
        .catch(e => {
          this.$message.warn('请求失败！')
          this.confirmLoading = false
        })
    },
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    onResetForm() {
      this.queryParam = {}
      this.pageFilter.pageNo = 1
      this.pageFilter.pageSize = 10
      this.refreshTable()
    },
    onSelectedRow(keys) {
      let record = keys && keys.length > 0 ? keys[0] : {}
      this.$set(this.model, 'belongManagerId', Object.keys(record).length > 0 && record.id ? record.id : '')
      this.$set(this.model, 'belongManagerName', Object.keys(record).length > 0 && record.personnelName ? record.personnelName : '')
      this.$set(this.model, 'belongManagerPhone', Object.keys(record).length > 0 && record.phone ? record.phone : '')
      this.$set(this.model, 'birthday', Object.keys(record).length > 0 && record.birthday ? record.birthday : '')
      this.$set(this.model, 'identification', Object.keys(record).length > 0 && record.identityCard ? record.identityCard : '')
      this.$set(this.model, 'post', Object.keys(record).length > 0 && record.duties ? record.duties : '')
      this.$set(this.model, 'gender', Object.keys(record).length > 0 && record.personnelSex ? record.personnelSex : '')
    },
    onTableChange(pagination, pageFilter, sorter) {
      this.pageFilter = Object.assign({}, this.pageFilter, pagination)
      this.refreshTable()
    },
    refreshTable() {
      // departId = this.selectedDepIds.join(',') //1584719851893985281
      let queryParam = Object.assign({}, this.queryParam, this.pageFilter)
      this.loading = true
      queryParam.division = this.departmentId
      getDivisionAdmin(queryParam).then(res => {
        if (res.success) {
          this.loading = false
          this.pageData = res.result
          this.$refs.table.onClearRowKeys()
        }
      }).catch(err => {

      })
      // getAction('/sys/user/list', queryParam)
      //   .then(res => {
      //     this.loading = false
      //     this.pageData = res.result ? res.result : {}
      //     this.$refs.table.onClearRowKeys()
      //     if (!res.success) {
      //       this.$message.warn(res.message?res.message:'请求失败')
      //     }
      //   })
      //   .catch(e => {
      //     this.loading = false
      //     this.$message.warn('请求失败！')
      //   })
    }
  }
}
</script>
